// pages/orderlist/orderdetail/orderdetail.js
var app = getApp();
var orderid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoLoaded:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    orderid = options.orderid;
  },
  loadOrderDetail: function (orderid) {
    var url = '/index.php/api/Order/order_detail';
    var params = {
      id: orderid
    };
    app.request(url,params).then(this.orderDetailCallBack.bind(this));
  },
  orderDetailCallBack: function (res) {
    var orderinfo = {};
    orderinfo.order_sn = res.result.order_info.order_sn;
    orderinfo.order_id = res.result.order_info.order_id;
    orderinfo.status = res.result.order_info.order_status_desc;
    orderinfo.status_code = res.result.order_info.order_status_code;
    orderinfo.orderdate = app.formatDateTime(res.result.order_info.add_time);
    orderinfo.reduction = res.result.order_info.coupon_price;
    orderinfo.totalprice = res.result.order_info.order_amount;
    orderinfo.order_amount = res.result.order_info.order_amount;
    orderinfo.price = res.result.order_info.goods_price;
    orderinfo.express = res.result.order_info.shipping_price;
    orderinfo.shipping_code = res.result.order_info.shipping_code;
    orderinfo.invoice_no = res.result.order_info.invoice_no;

    orderinfo.pay_btn = res.result.order_info.pay_btn;
    orderinfo.cancel_btn = res.result.order_info.cancel_btn;
    orderinfo.receive_btn = res.result.order_info.receive_btn;
    orderinfo.comment_btn = res.result.order_info.comment_btn;
    orderinfo.shipping_btn = res.result.order_info.shipping_btn;
    orderinfo.return_btn = res.result.order_info.return_btn;
    // 
    var showBtn = true;
    if (orderinfo.pay_btn.toString() != '1' && orderinfo.cancel_btn.toString() != '1' && orderinfo.comment_btn.toString() != '1' && orderinfo.shipping_btn.toString() != '1' && orderinfo.receive_btn.toString() != '1') {
      showBtn = false;
    }
    orderinfo.address = {
      name: res.result.order_info.consignee,
      phone: res.result.order_info.mobile,
      address: res.result.order_info.address
    };
    var goodslist = [];
    var gNames = [];
    var goods_list = res.result.order_info.goods_list;
    for (var i = 0; i < goods_list.length; i++) {
      var goods = {};
      goods.goodsid = goods_list[i].goods_id;
      goods.name = goods_list[i].goods_name;
      goods.buycount = goods_list[i].goods_num;
      goods.oprice = goods_list[i].market_price;
      goods.dprice = goods_list[i].member_goods_price;
      goods.icon = app.globalData.webUrl + goods_list[i].original_img;
      goods.recid = goods_list[i].rec_id;
      goods.speckey = goods_list[i].spec_key;
      gNames.push(goods.name);
      var standard = [];
      if (goods_list[i].spec_key_name != '' && goods_list[i].spec_key_name != null) {
        standard = goods_list[i].spec_key_name.split(' ');
        for (var k = 0; k < standard.length; k++) {
          standard[k] = standard[k].split(':')[1];
        }
      }
      goods.standard = standard;
      goodslist.push(goods);
    }
    orderinfo.gnames = gNames.join(',');
    orderinfo.goods = goodslist;
    this.setData({
      showBtn: showBtn,
      orderinfo: orderinfo,
      infoLoaded: true
    });

    app.hideLoading();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.showLoading();
    app.ready(() => {
      app.showLoading();
      this.loadOrderDetail(orderid);
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  goodsBackOff: function (event) {
    var goods = event.currentTarget.dataset.goods;
    var orderid = event.currentTarget.dataset.orderid;
    var ordersn = event.currentTarget.dataset.ordersn;
    goods.orderid = orderid;
    goods.ordersn = ordersn;
    wx.navigateTo({
      url: '../backoff/backoff?goods=' + JSON.stringify(goods),
    });
  },
  dealOrder: function (event) {
    var orderinfo = event.currentTarget.dataset.orderinfo;
    var orderid = orderinfo.order_id;
    var dealtype = event.currentTarget.dataset.dealtype;
    var that = this;
    if (dealtype == 'express') {
      wx.navigateTo({
        url: '../express/express?shippingcode=' + orderinfo.shipping_code + '&invoiceno=' + orderinfo.invoice_no,
      });
    }
    else if (dealtype == 'comment') {
      wx.navigateTo({
        url: '../commentlist/commentlist?orderid=' + orderid,
      })
    }
    else if (dealtype == 'pay') {
      wx.navigateTo({
        url: '../payorder/payorder?order=' + JSON.stringify(orderinfo),
      });
    }
    else if (dealtype == 'cancel') {
      if (orderinfo.status_code == 'WAITSEND'){
        wx.navigateTo({
          url: '../cancelafterpay/cancelafterpay?order=' + JSON.stringify(orderinfo),
        });
        return;
      }
      wx.showModal({
        title: '提示',
        content: '确定取消该订单吗',
        success: function (res) {
          if (res.confirm) {
            that.updateOrder(orderinfo.order_id, 'cancel');
          }
        }
      });
    }
    else if (dealtype == 'receive') {
      wx.showModal({
        title: '提示',
        content: '确定取消该订单吗',
        success: function (res) {
          if (res.confirm) {
            that.updateOrder(orderinfo.order_id, 'receive');
          }
        }
      });
    }
  },
  updateOrder: function (orderid, dtype) {
    var url = '';
    if (dtype == 'cancel') {
      url = '/index.php/api/Order/cancel_order';
    }
    else if (dtype == 'receive') {
      url = '/index.php/api/Order/order_confirm';
    }
    var params = {
      id: orderid
    };
    app.request(url,params).then(this.updateOrderCallBack.bind(this));
  },
  updateOrderCallBack: function (res) {
    wx.showToast({
      title: res.msg,
      icon: 'none'
    });
    if (res.status == 'ok') {
      app.showLoading();
      this.loadOrderDetail(orderid);
    }
  }
})